#!/bin/sh
pid=.bot.pid
program=./bot.tcl
export LC_CTYPE=ru_RU.UTF-8

say () { echo "$@" >&2; }

start () {
  say -n Starting: bot
	umask 022
  "tclsh" bot.tcl >taho.log 2>&1 &
  echo $! > "$pid"
  say .
}

stop () {
  say -n Stopping: bot
  if [ -f "$pid" ]; then
    kill "$(<$pid)"
    rm "$pid"
  else
    say $program is not running
  fi
  say .
}

status () {
  if [ -f "$pid" ]; then
    if [ -e "/proc/$(<"$pid")" ]; then
      say $program is runnig
    else
      say $program is not running, but the pidfile exists
    fi
  else
    say $program is not running
  fi
}

case "$1"  in
  start) start;;
  stop) stop;;
  restart) stop; start;;
  status) status;;
esac
